package com.tang.sharding.algorithm;

import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.RangeShardingValue;

import java.util.Collection;

/**
 * @author Tang
 * @classname AbstractShardingAlgorithm
 * @description [  ]
 * @date 2021/8/3 15:25
 */
public abstract class AbstractShardingAlgorithm<T extends Comparable<?>> implements BaseShardingAlgorithm<T> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<T> shardingValue) {
        String tableName = doPreciseSharding(shardingValue);
        if (availableTargetNames.contains(tableName)) {
            return tableName;
        }
        throw new RuntimeException("需要操作的表不存在!");
    }

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<T> shardingValue) {
        Collection<String> tableNames = doRangeSharding(shardingValue);
        if (availableTargetNames.containsAll(tableNames)) {
            return tableNames;
        }
        throw new RuntimeException("需要操作的表不存在!");
    }

}